/**
 * This Class implements the interface Player.
 * 
 * 
 * 
 * @mathmodel A player holds the information for the player such as, the score,
 *            # of wins, and the secretWord
 * 
 * @mathdef three fields int score int wins String secretWord
 * 
 * @initally score = 0; wins = 0; secretWord = null;
 * 
 * @author Brian
 */
public class ActivePlayer implements Player {

	/**
	 * Holds the players current score.
	 */
	private int score = 0;

	/**
	 * Holds the players current # of wins.
	 */
	private int wins = 0;

	/**
	 * Holds the players secret word.
	 */
	private String secretWord;

	/**
	 * The dictionary used to compare words.
	 */
	private AbstractDictionary dict;

	/**
	 * Instantiates this object with the given dictionary.
	 * 
	 * @param dict
	 *            the dictionary to use for word comparison.
	 */
	public ActivePlayer(AbstractDictionary dict) {
		this.dict = dict;
	}

	// Methods

	/**
	 * Compares a word to this player's secret word and returns the number of
	 * shared letters.
	 * 
	 * @see Player#guess(Player, java.lang.String)
	 * 
	 * @return int number of letters shared by the words
	 * @param sharedLetters number of letters in common
	 * @param dict
	 * @param guess
	 *            the word being compared
	 * @throws WordException throws this to the controller
	 */
	@Override
	public boolean guess(String guess, int[] sharedLetters)
			throws WordException {

		try {
			sharedLetters[0] = dict.compareWords(guess, this.secretWord);
		} catch (WordException e) {
			throw (e);
		}

		boolean ans = false;
		final int wordLENGTH = 	5;
		if (sharedLetters[0] == wordLENGTH) {
			String tmp = guess.toLowerCase();
			if (tmp.equalsIgnoreCase(this.secretWord)) {
				ans = true;
			}
		}
		return ans;
	}

	/**
	 * Gets this player's point score.
	 * 
	 * @see Player#score()
	 * @return the number of points this player has.
	 */
	@Override
	public int getScore() {
		return this.score;
	}

	/**
	 * @see Player#setWord(java.lang.String)
	 * @param word the word to be set
	 * @throws WordException if word is not in dictionary
	 */
	@Override
	public void setWord(String word) throws WordException {

		if (dict.containsWord(word)) {
			this.secretWord = word.toLowerCase();
		} else {
		throw (new WordException(WordProblem.NotInDict));
		}
	}

	/**
	 * @see Player#win(int)
	 * @param score the score to be added
	 */
	@Override
	public void win(int score) {
		this.wins++;
		this.score += score;
	}

	/**
	 * @see Player#lose(int)
	 * @param score the score to be added
	 */
	public void lose(int score) {
		this.score += score;
	}

	// Private Methods

	/**
	 * @see Player#reset()
	 */
	public void reset() {
		this.score = 0;
		this.secretWord = null;
		this.wins = 0;
	}

	/**
	 * @see Player#getWins()
	 * @return # of wins
	 */
	public int getWins() {
		return this.wins;
	}
}
